
package com.gzbsoft.sc.modules.sys.service.impl;
import com.gzbsoft.sc.modules.sys.dao.SysMenuDao;
import com.gzbsoft.sc.modules.sys.entity.SysMenu;
import com.gzbsoft.sc.modules.sys.service.SysMenuService;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu, Long> implements SysMenuService {
    private final SysMenuDao sysMenuDao;
    public SysMenuServiceImpl(SysMenuDao sysMenuDao) {
        this.sysMenuDao = sysMenuDao;
    }
    @Override
    public void deleteById(Long menuId) {
        super.deleteById(menuId);
    }
    @Override
    public List<SysMenu> menuTree(@NotNull Long currId) {
        List<SysMenu> sysMenuList = sysMenuDao.findChildren(currId);
        return sysMenuList.stream().peek(menu -> {
            menu.setChildren(filterMenu(menu.getId(), sysMenuList));
        }).filter(menu -> Objects.equals(menu.getParent().getId(), currId)).collect(Collectors.toList());
    }
    private List<SysMenu> filterMenu(@NotNull Long currId, @NotNull List<SysMenu> menuList) {
        return menuList.stream().filter(menu -> Objects.equals(menu.getParent().getId(), currId)).collect(Collectors.toList());
    }
}
